package com.igfd.tanmao.utils;


import com.igfd.tanmao.pojo.TanmaoOrderDetailResult;
import com.igfd.tanmao.pojo.TanmaoOrderResult;
import com.igfd.tanmao.pojo.TanmaoTradesSoldGetResult;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class JsonUtils {

    public static long parseTanmaoItemsOnsaleRsp(String rspJson,ArrayList<String> numIids) {
        long totalPage = 0;
        JSONObject jsonObject = JSONObject.fromObject(rspJson);

        Object items_onsale_get_response = jsonObject.get("items_onsale_get_response");
        if (items_onsale_get_response != null && items_onsale_get_response != "") {
            Object items = jsonObject.getJSONObject("items_onsale_get_response").get("items");
            totalPage = Long.valueOf(String.valueOf(jsonObject.getJSONObject("items_onsale_get_response").get("total_results")));;

            if (items != null && items != "") {
                JSONArray jsonArray = jsonObject.getJSONObject("items_onsale_get_response").getJSONObject("items").getJSONArray("item");
                if (jsonArray != null && jsonArray.size() > 0) {
                    for (int i = 0; i < jsonArray.size(); i++) {

                        String numIid = String.valueOf(jsonArray.getJSONObject(i).get("num_iid"));
                        numIids.add(numIid);

                    }
                }
            }
        }

        return totalPage;
    }

    public static ArrayList<Map<String,String>> parseTanmaoItemSkusRsp(String rspJson) {
        ArrayList<Map<String,String>> resultList = new ArrayList<Map<String,String>>();

        JSONObject jsonObject = JSONObject.fromObject(rspJson);
        Object item_skus_get_response = jsonObject.get("item_skus_get_response");

        if (item_skus_get_response != null && String.valueOf(item_skus_get_response) != "") {

            Object skus = jsonObject.getJSONObject("item_skus_get_response").get("skus");
            if (skus != null && String.valueOf(skus) != "") {
                JSONArray jsonArray = jsonObject.getJSONObject("item_skus_get_response").getJSONObject("skus").getJSONArray("sku");

                if (jsonArray != null && jsonArray.size() > 0) {
                    for (int i = 0; i < jsonArray.size(); i++) {

                        JSONObject skuObj = JSONObject.fromObject(jsonArray.get(i));

                        String propertiesName = String.valueOf(skuObj.get("properties_name"));
                        String skuId = String.valueOf(skuObj.get("sku_id"));

                        if (!propertiesName.isEmpty() && !skuId.isEmpty()) {
                            HashMap<String, String> hashMap = new HashMap<>();

                            hashMap.put("propertiesName",propertiesName);
                            hashMap.put("skuId",skuId);

                            resultList.add(hashMap);
                        }
                    }
                }
            }
        }

        return resultList;
    }

    public static TanmaoTradesSoldGetResult parseTanmaoOrderInfoRsp(String rspJson) {
        TanmaoTradesSoldGetResult rspResult = new TanmaoTradesSoldGetResult();

        JSONObject jsonObject = JSONObject.fromObject(rspJson);

        Object trades_sold_get_response = jsonObject.get("trades_sold_get_response");
        if (trades_sold_get_response != null && String.valueOf(trades_sold_get_response) != "") {

            Object trades = jsonObject.getJSONObject("trades_sold_get_response").get("trades");
            boolean hasNext = Boolean.valueOf(String.valueOf(jsonObject.getJSONObject("trades_sold_get_response").get("has_next")));
            rspResult.setHasNext(hasNext);

            if (trades != null && String.valueOf(trades) != "") {
                JSONArray tradeArray = jsonObject.getJSONObject("trades_sold_get_response").getJSONObject("trades").getJSONArray("trade");

                if (tradeArray != null && tradeArray.size() > 0) {

                    ArrayList<TanmaoOrderResult> orderArrayList = new ArrayList<TanmaoOrderResult>();

                    for (int i = 0; i < tradeArray.size(); i++) {

                        TanmaoOrderResult tanmaoOrderResult = new TanmaoOrderResult();

                        JSONObject tradeObj = JSONObject.fromObject(tradeArray.get(i));

                        String created = "";
                        if (tradeObj.get("created") != null) {
                            created = String.valueOf(tradeObj.get("created"));
                        }

                        String pay_time = "";
                        if (tradeObj.get("pay_time") != null) {
                            pay_time = String.valueOf(tradeObj.get("pay_time"));
                        }

                        String receiver_address = "";
                        if (tradeObj.get("receiver_address") != null) {
                            receiver_address = String.valueOf(tradeObj.get("receiver_address"));
                        }

                        String receiver_mobile = "";
                        if (tradeObj.get("receiver_mobile") != null) {
                            receiver_mobile = String.valueOf(tradeObj.get("receiver_mobile"));
                        }

                        String receiver_name = "";
                        if (tradeObj.get("receiver_name") != null) {
                            receiver_name = String.valueOf(tradeObj.get("receiver_name"));
                        }

                        String receiver_state = "";
                        if (tradeObj.get("receiver_state") != null) {
                            receiver_state = String.valueOf(tradeObj.get("receiver_state"));
                        }

                        String status = "";
                        if (tradeObj.get("status") != null) {
                            status = String.valueOf(tradeObj.get("status"));
                        }

                        String tid = "";
                        if (tradeObj.get("tid") != null) {
                            tid = String.valueOf(tradeObj.get("tid"));
                        }

                        tanmaoOrderResult.setCreated(created);
                        tanmaoOrderResult.setPayTime(pay_time);
                        tanmaoOrderResult.setReceiverAddress(receiver_address);
                        tanmaoOrderResult.setReceiverMobile(receiver_mobile);
                        tanmaoOrderResult.setReceiverName(receiver_name);
                        tanmaoOrderResult.setReceiverState(receiver_state);
                        tanmaoOrderResult.setStatus(status);
                        tanmaoOrderResult.setTid(tid);

                        Object orders = tradeObj.get("orders");
                        if (orders != null && !String.valueOf(orders).isEmpty()) {

                            JSONObject ordersObj = JSONObject.fromObject(orders);
                            Object order = ordersObj.get("order");

                            if (order != null && !String.valueOf(order).isEmpty()) {
                                JSONArray orderArray = ordersObj.getJSONArray("order");

                                if (orderArray != null && orderArray.size() > 0) {

                                    ArrayList<TanmaoOrderDetailResult> orderDetailArr = new ArrayList<TanmaoOrderDetailResult>();

                                    for (int j = 0; j < orderArray.size(); j++) {
                                        JSONObject orderObj = JSONObject.fromObject(orderArray.get(j));

                                        String oid = "";
                                        if (orderObj.get("oid") != null) {
                                            oid = String.valueOf(orderObj.get("oid"));
                                        }

                                        String title = "";
                                        if (orderObj.get("title") != null) {
                                            title = String.valueOf(orderObj.get("title"));
                                        }

                                        String num_iid = "";
                                        if (orderObj.get("num_iid") != null) {
                                            num_iid = String.valueOf(orderObj.get("num_iid"));
                                        }

                                        String sku_id = "";
                                        if (orderObj.get("sku_id") != null) {
                                            sku_id = String.valueOf(orderObj.get("sku_id"));
                                        }

                                        String num = "";
                                        if (orderObj.get("num") != null) {
                                            num = String.valueOf(orderObj.get("num"));
                                        }

                                        String price = "";
                                        if (orderObj.get("price") != null) {
                                            price = String.valueOf(orderObj.get("price"));
                                        }

                                        String total_fee = "";
                                        if (orderObj.get("total_fee") != null) {
                                            total_fee = String.valueOf(orderObj.get("total_fee"));
                                        }

                                        String payment = "";
                                        if (orderObj.get("payment") != null) {
                                            payment = String.valueOf(orderObj.get("payment"));
                                        }

                                        String discount_fee = "";
                                        if (orderObj.get("discount_fee") != null) {
                                            discount_fee = String.valueOf(orderObj.get("discount_fee"));
                                        }

                                        String adjust_fee = "";
                                        if (orderObj.get("adjust_fee") != null) {
                                            adjust_fee = String.valueOf(orderObj.get("adjust_fee"));
                                        }

                                        String sku_properties_name = "";
                                        if (orderObj.get("sku_properties_name") != null) {
                                            sku_properties_name = String.valueOf(orderObj.get("sku_properties_name"));
                                        }

                                        TanmaoOrderDetailResult tanmaoOrderDetailResult = new TanmaoOrderDetailResult();

                                        tanmaoOrderDetailResult.setOid(oid);
                                        tanmaoOrderDetailResult.setTitle(title);
                                        tanmaoOrderDetailResult.setNumIid(num_iid);
                                        tanmaoOrderDetailResult.setSkuId(sku_id);
                                        tanmaoOrderDetailResult.setTotalQuantity(num);
                                        tanmaoOrderDetailResult.setPrice(price);
                                        tanmaoOrderDetailResult.setTotalFee(total_fee);
                                        tanmaoOrderDetailResult.setPayment(payment);
                                        tanmaoOrderDetailResult.setDiscountFee(discount_fee);
                                        tanmaoOrderDetailResult.setAdjustFee(adjust_fee);
                                        tanmaoOrderDetailResult.setSkuPropertiesName(sku_properties_name);

                                        orderDetailArr.add(tanmaoOrderDetailResult);
                                    }

                                    tanmaoOrderResult.setOrders(orderDetailArr);
                                    orderArrayList.add(tanmaoOrderResult);
                                }
                            }
                        }
                    }

                    rspResult.setOrder(orderArrayList);
                }
            }
        }

        return rspResult;
    }
}
